'use strict';
const {verifyToken} =require("wx-common") 

exports.main = async (event, context) => {
	console.log('event',event)
	//event为客户端上传的参数
	const {token}=event
	const db=uniCloud.database()
	const dbCmd=db.command
	const payload =event.token?verifyToken(event.token):null
	
	let dbRes;
	if (event.action=="create"){
		dbRes=await db.collection('meno').add({
			owner:payload.openid,
			text:event.text,
			date:event.date
		})
	} else if (event.action=="query"){
		dbRes=await db.collection("meno").where({
			owner:dbCmd.eq(payload.openid)
		}).orderBy('_id','desc').get()
	} else if (event.action=="delete"){
		dbRes=await db.collection("meno").where({
			owner:dbCmd.eq(payload.openid),
			"_id":dbCmd.eq(event.id),
		}).remove()
	} else if (event.action='update'){
		 dbRes= await db.collection('meno').where({
			owner:dbCmd.eq(payload.openid),
			"_id":dbCmd.eq(event.id),
		}).update({
			owner:payload.openid,
			text:event.text,
			date:new Date().toLocaleString()
		})
	}
	
	return dbRes
};
